﻿using System.Security.Cryptography;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using PaymentServiceFactory.Data;
using PaymentServiceFactory.Services;
using static System.Net.Mime.MediaTypeNames;

namespace PaymentServiceFactory.Controllers
{
    //[Route("api/[controller]")]
    //[ApiController]
    //public class PaymentController : ControllerBase
    //{
    //    private readonly PaymentProcessor _paymentProcessor;
    //    public PaymentController(PaymentProcessor paymentProcessor)
    //    {
    //        _paymentProcessor = paymentProcessor;
    //    }

    //    [HttpPost]
    //    public IActionResult ProcessPayment([FromQuery] string serviceType, [FromQuery] decimal amount)
    //    {
    //        if (string.IsNullOrEmpty(serviceType) || amount <= 0)
    //        {
    //            return BadRequest("Invalid payment service type or amount.");
    //        }
    //        var result = _paymentProcessor.ProcessPayment(serviceType, amount);
    //        return Ok(new
    //        {
    //            PaymentType = serviceType,
    //            Amount = amount,
    //            Result = result
    //        });
    //    }
    //}
    [ApiController]
    [Route("api/[controller]")]
    public class PaymentController : ControllerBase
    {
        private readonly PaymentProcessor _paymentProcessor;
        private readonly AppDbContext _context;

        public PaymentController(PaymentProcessor paymentProcessor, AppDbContext context)
        {
            _paymentProcessor = paymentProcessor;
            _context = context;
        }

        [HttpPost("{orderId}")]
        public async Task<IActionResult> ProcessPayment(
    string orderId,
        [FromQuery] string type,
        [FromBody] decimal amount)
        {
            try
            {

//                POST / api / payment / ORD - 12345 ? type = PayPal
//Content - Type: application / json
                var result = await _paymentProcessor.ProcessPaymentAsync(type, orderId, amount);
                return Ok(new { OrderId = orderId, PaymentType = type, Result = result });
            }
            catch (KeyNotFoundException ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}
